<div class="left-sidebar dis-flex">
    <!-- 一级菜单 -->
    <ul id="parent_menu" class="sidebar-nav">
        <li class="sidebar-nav-heading">琅氏·颌楽</li>
        <li class="sidebar-nav-link">
            <a id="index" href="/admin/index.html"
               class="active">
                <i class="iconfont sidebar-nav-link-logo icon-home"
                   style=""></i>
                首页                    </a>
        </li>
    </ul>
    <!-- 子级菜单-->
    <ul  class="left-sidebar-second" >
        <li class="sidebar-second-title"></li>
        <li class="sidebar-second-item"></li>
    </ul>
    <!-- 子级菜单-->
</div>
<script src="Scripts/func.js"></script>
<script src="Scripts/api.js"></script>
<script language="JavaScript">
    menus(function (data) {
        localStorage.setItem("menus",JSON.stringify(data));
        var templete = ' <li class="sidebar-nav-link">\n' +
            '            <a id="menu{0}" href="javascript:showSub({0})">\n' +
            '                <i class="iconfont sidebar-nav-link-logo {2}"\n' +
            '                   style=""></i>\n' +
            '                {1} </a>\n' +
            '        </li>';

        // if(res.data.length>0)
        // $("#parent_menu").append(StringFormat(templete, 0, res.data[0].name,"active"));
        for (var i = 0; i < data.length; i++) {
            var div = StringFormat(templete, i, data[i].name,data[i].icon);
            $("#parent_menu").append(div);
        }
    })

    function showSub($index) {
        $(".left-sidebar-second").attr("style","");
        $(".sidebar-second-item").empty();
        var menus = JSON.parse(localStorage.getItem("menus"));

        if($index>menus.length)
            return;
        changeParent($index,menus.length);
        var subs = menus[$index].nodes;
        $(".sidebar-second-title").text(menus[$index].name);
        var templetes = '<a href="{0}"\n target="content"' +
            '                   class="{1}">\n' +
            '                    {2} </a>\n';
        if(subs.length>0)
        $(".sidebar-second-item").append(StringFormat(templetes,subs[0].url,"active",subs[0].name));
        for (var i = 1; i < subs.length; i++) {
            $(".sidebar-second-item").append(StringFormat(templetes,subs[i].url,"",subs[i].name));
        }
        // loadContent(subs[0].url);
    }

    function changeParent($index,$count) {
        $("#index").attr("class","");
        for (var i = 0; i < $count; i++) {
            $("#menu"+i).attr("class","");
        }
        $("#menu"+$index).attr("class","active");
    }


</script>
